/*
 * Software Author: Mike Moore Vectrics Flashrecruit Open source job board and
 * applicant tracking system. Built with enterprise Java. GPL License applies to
 * this source code.
 */
package org.vectrics.struts;

import java.util.Locale;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.vectrics.SystemException;
import org.vectrics.domain.Domain;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.user.Person;

/**
 * @author MMoore
 * 
 *         TODO To change the template for this generated type comment go to
 *         Window - Preferences - Java - Code Style - Code Templates
 */
public class VectricsActionData {
    private Person authPerson = null;

    private Request request = null;

    private HttpServletResponse response = null;

    private ActionMapping mapping = null;

    private int instanceNumber = 0;

    private ActionForm form = null;

    private ActionMessages actionErrors = null;

    /**
	 * 
	 */
    public VectricsActionData(Request request, ActionMapping mapping, ActionForm form, HttpServletResponse response) {
        super();
        this.request = request;
        this.response = response;
        this.mapping = mapping;
        this.form = form;
        Domain domain = CoreServiceLocator.getUserAuthService().getSessionDomain(request);
        authPerson = CoreServiceLocator.getUserAuthService().getAuthorizedPerson(domain, request);

        // TODO Auto-generated constructor stub
    }

    /**
     * @return Returns the request.
     */
    public Request getRequest() {
        return request;
    }

    public HttpServletResponse getResponse() {
        return response;
    }

    public void setRequestForm(ActionForm form) {
        if (form == null)
            throw new SystemException("form was passed as null to this method.");
        request.setAttribute(mapping.getAttribute(), form);
    }

    public void setSessionForm(ActionForm form) {
        request.setSessionAttribute(mapping.getAttribute(), form);
    }

    public void setForm(ActionForm form) {
        if ("request".equals(mapping.getScope())) {
            request.setAttribute(mapping.getAttribute(), form);
        } else {
            request.setSessionAttribute(mapping.getAttribute(), form);
        }
    }

    /**
     * @return Returns the mapping.
     */
    public ActionMapping getMapping() {
        return mapping;
    }

    public Locale getLocale() {
        return (request.getLocale());
    }

    public boolean wasButtonPressed(String name) {
        boolean result = false;
        String paramValue = request.getParameter(name);
        if (paramValue == null) {
            paramValue = request.getParameter(name + ".x");
        }
        if (paramValue != null)
            result = true;
        return (result);
    }

    public String getPhrase(String code) {
        return CoreServiceLocator.getPhraseProvider().getMessage(code, this.getLocale());
    }

    public Person getAuthPerson() {
        return (authPerson);
    }

    public Domain getAuthDomain() {
        return CoreServiceLocator.getUserAuthService().getSessionDomain(this.getRequest());
    }

    public ActionForm getForm() {
        return (this.form);
    }

    public void addError(String field, String phraseCode) {
        ActionMessage actionError = new ActionMessage(phraseCode);
        if (actionErrors == null)
            actionErrors = new ActionMessages();
        actionErrors.add(field, actionError);
    }

    public ActionMessages getErrors() {
        return (actionErrors);
    }

    public void saveForm(ActionForm form) {
        this.form = form;
        if ("request".equals(mapping.getScope())) {
            request.setAttribute(mapping.getAttribute(), form);
        } else {
            request.setSessionAttribute(mapping.getAttribute(), form);
        }
    }

}
